package com.hzkq.widgets.chart.simple;

import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint.Align;
import android.os.Bundle;
import android.widget.Button;

import com.hzkq.widgets.R;
import com.keqiang.chart.impl.axis.Label;
import com.keqiang.chart.impl.axis.XAxisHorizontal;
import com.keqiang.chart.impl.axis.YAxisHorizontal;
import com.keqiang.chart.impl.axis.impl.bar.BarEntity;
import com.keqiang.chart.impl.axis.impl.bar.horizontal.BarChartHorizontal;
import com.keqiang.chart.impl.axis.impl.bar.horizontal.BarChartHorizontalHighLightRender;
import com.keqiang.chart.impl.axis.impl.bar.horizontal.BarChartHorizontalView;
import com.keqiang.chart.impl.axis.impl.bar.horizontal.BarData;
import com.keqiang.chart.impl.axis.impl.bar.horizontal.BarGroup;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;
import me.zhouzhuo810.magpiex.ui.act.BaseActivity;
import me.zhouzhuo810.magpiex.ui.widget.TitleBar;
import me.zhouzhuo810.magpiex.utils.SimpleUtil;

/**
 * @author Created by wanggaowan on 2022/7/26 15:00
 */
public class BarChartHorizontalActivity extends BaseActivity {
    
    private TitleBar mTitleBar;
    private BarChartHorizontal mBarChart2;
    private BarChartHorizontal mBarChart;
    private Button mBtRefresh;
    
    @Override
    public int getLayoutId() {
        return R.layout.activity_simple_bar_chart_horizontal;
    }
    
    @Override
    public boolean shouldSupportMultiLanguage() {
        return false;
    }
    
    @Override
    public void initView(@Nullable Bundle savedInstanceState) {
        mTitleBar = findViewById(R.id.title_bar);
        BarChartHorizontalView barChartView = findViewById(R.id.bar_chart);
        mBarChart = barChartView.getChart();
        BarChartHorizontalView barChartView2 = findViewById(R.id.bar_chart2);
        mBarChart2 = barChartView2.getChart();
        mBtRefresh = findViewById(R.id.bt_refresh);
        
        initBarChart();
        initBarChart2();
    }
    
    @Override
    public void initData() {
        fillBarChartData();
        fillBarChartData2();
    }
    
    @Override
    public void initEvent() {
        mTitleBar.getLlLeft().setOnClickListener(v -> closeAct());
        
        mBtRefresh.setOnClickListener(v -> fillBarChartData2());
    }
    
    private void initBarChart() {
        XAxisHorizontal xAxis = mBarChart.getXAxis();
        xAxis.setDrawGridLine(true);
        xAxis.setLineColor(Color.TRANSPARENT);
        xAxis.setGridUseEndPadding(true);
        xAxis.setLabelAlign(Align.CENTER);
        xAxis.setLabelOverAlign(Align.LEFT);
        xAxis.setLabelCount(6);
        xAxis.setLabelMaxLines(2);
        xAxis.setGridLinePathEffect(new DashPathEffect(new float[]{10f, 10f}, 0));
        xAxis.setLabelOffsetAxis(SimpleUtil.getScaledValue(10));
        xAxis.setEndPadding(SimpleUtil.getScaledValue(60));
        xAxis.setMin(0);
        xAxis.setMax(100);
        xAxis.setAxisValueFormat(integer -> Label.obtain());
        
        YAxisHorizontal yAxis = mBarChart.getYAxis();
        yAxis.setDrawGridLine(false);
        yAxis.setGridUseEndPadding(true);
        yAxis.setLabelAlign(Align.RIGHT);
        yAxis.setLabelMaxLines(2);
        yAxis.setLabelOffsetAxis(SimpleUtil.getScaledValue(10));
        yAxis.setEndPadding(0);
        yAxis.setLabelMaxWidth(SimpleUtil.getScaledValue(100f));
        yAxis.setLabelCount(6);
        yAxis.setLabelMinWidth(SimpleUtil.getScaledValue(60));
        
        yAxis.setAxisValueFormat(integer -> Label.obtain((integer + 1) + "月"));
        
        mBarChart.setHighLightRender(new BarChartHorizontalHighLightRender(mBarChart));
    }
    
    private void initBarChart2() {
        XAxisHorizontal xAxis = mBarChart2.getXAxis();
        xAxis.setDrawGridLine(true);
        xAxis.setGridUseEndPadding(true);
        xAxis.setLabelAlign(Align.CENTER);
        xAxis.setLabelOverAlign(Align.LEFT);
        xAxis.setLabelCount(11);
        xAxis.setLabelMaxLines(2);
        xAxis.setLabelOffsetAxis(SimpleUtil.getScaledValue(10));
        xAxis.setEndPadding(SimpleUtil.getScaledValue(30));
        xAxis.setMin(0);
        xAxis.setMax(200);
        
        YAxisHorizontal yAxis = mBarChart2.getYAxis();
        yAxis.setDrawGridLine(true);
        yAxis.setGridUseEndPadding(true);
        yAxis.setLabelAlign(Align.RIGHT);
        yAxis.setLabelMaxLines(2);
        yAxis.setLabelOffsetAxis(SimpleUtil.getScaledValue(10));
        yAxis.setEndPadding(SimpleUtil.getScaledValue(30));
        yAxis.setLabelMaxWidth(SimpleUtil.getScaledValue(100f));
        yAxis.setLabelCount(6);
        yAxis.setLabelMinWidth(SimpleUtil.getScaledValue(60));
        
        yAxis.setAxisValueFormat(integer -> Label.obtain((integer + 1) + "月"));
        
        mBarChart2.setHighLightRender(new BarChartHorizontalHighLightRender(mBarChart2));
    }
    
    /**
     * 设置图表数据
     */
    private void fillBarChartData() {
        List<BarGroup> data = new ArrayList<>();
        int count = mBarChart.getYAxis().getLabelCount();
        XAxisHorizontal xAxis = mBarChart.getXAxis();
        for (int i = 0; i < count; i++) {
            List<BarEntity> entityList = new ArrayList<>();
            BarEntity entity = new BarEntity((float) (xAxis.getMin() + Math.random() * (xAxis.getMax() - xAxis.getMin())));
            entity.setDrawLabel(true);
            entity.setWidthPercent(0.5f);
            entity.setColor(0xff61F2AD);
            entity.setBgColor(0x1001297D);
            entity.setLabelFixTop(true);
            entity.setLabel(((int) entity.getValue()) + "");
            entityList.add(entity);
            BarGroup barData = new BarGroup(entityList);
            barData.setBarOffset(SimpleUtil.getScaledValue(5));
            data.add(barData);
        }
        
        BarData barData = new BarData();
        barData.setGroups(data);
        mBarChart.setData(barData);
        mBarChart.animDraw(2000);
    }
    
    /**
     * 设置图表数据
     */
    private void fillBarChartData2() {
        List<BarGroup> data = new ArrayList<>();
        int count = mBarChart2.getYAxis().getLabelCount();
        XAxisHorizontal xAxis = mBarChart2.getXAxis();
        for (int i = 0; i < count; i++) {
            List<BarEntity> entityList = new ArrayList<>();
            for (int j = 0; j < 2; j++) {
                BarEntity entity = new BarEntity((float) (xAxis.getMin() + Math.random() * (xAxis.getMax() - xAxis.getMin())));
                entity.setDrawLabel(true);
                entity.setLabel(((int) entity.getValue()) + "");
                if (j == 0) {
                    entity.setWidthPercent(0.5f);
                    entity.setColor(0xff61F2AD);
                } else {
                    entity.setWidthPercent(0.3f);
                    entity.setColor(0xffFF6600);
                }
                entityList.add(entity);
            }
            BarGroup barData = new BarGroup(entityList);
            barData.setBarOffset(SimpleUtil.getScaledValue(5));
            data.add(barData);
        }
        
        BarData barData = new BarData();
        barData.setGroups(data);
        mBarChart2.setData(barData);
        mBarChart2.animDraw(2000);
    }
}
